package com.hnevc.nbastarserver.controller;


import com.hnevc.nbastarserver.bean.Ball;
import com.hnevc.nbastarserver.dao.BallDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/Ball")
public class BallController {
        @Autowired
        BallDao ballDao;


    @RequestMapping("/list")
    @ResponseBody
        public Map<String, Object> Ball() {
            Map<String, Object> map = new HashMap<>();
            List<Ball> ballList = ballDao.findAll();
            if (ballList == null) {
                map.put("success", false);
                map.put("msg", "失败");
            } else {
                map.put("success", true);
                map.put("msg", "成功");
                map.put("list", ballList);
            }
            return map;
    }

    //add
    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody Ball ball) {
        Map<String, Object> map = new HashMap<>();
        Ball  ball1 = ballDao.saveAndFlush(ball);
        if (ball1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data",ball1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @RequestMapping("/findone/{id}")
    @ResponseBody
    public Map<String,Object>findoneById(@PathVariable Integer id) {
        Map<String, Object> map = new HashMap<>();
        Optional<Ball> video1 = ballDao.findById(id);
        if (video1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", video1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
}